/***************************************************************************
 *   oboe.controllers.jml.helpers.list.TextList                            *
 *   Copyright (C) 2011 by Felipe Manga                                    *
 *   lufeboma@gmail.com                                                    *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

package oboe.controllers.jml.helpers.list
{
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import oboe.controllers.jml.events.JMLEvent;
	import oboe.controllers.jml.helpers.JMLClipper;
	
	public class TextList extends JMLClipper
	{
		private var items:Vector.<TextField>;
		private var nextY:Number;
		public var fillColor:int = 0xEFEFEF;
		public var linePadding:int = 5;
		
		public function TextList()
		{
			super();
			this.items = new Vector.<TextField>();
			this.nextY = 0;
			Object(this).addCallback( JMLEvent.JMLCOMPLETE, redraw );
			Object(this).addCallback( "changeSize", redraw );
		}

		private function redraw( ):void
		{
			this.graphics.clear();
			this.graphics.beginFill( fillColor, 1 );
			this.graphics.drawRect( 0, 0, this.width, this.height );
			this.graphics.endFill();
		}
		
		public function getItemCount():int
		{
			return this.items.length;
		}
		
		public function pushItem( str:String ):int
		{
			var tf:TextField = new TextField();
			items.push( tf );
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.text = str;
			tf.y = nextY;
			this.addChild( tf );
			nextY += tf.height + linePadding;
			this.dispatchEvent( new Event("changeSize") );
			return items.length - 1;
		}
		
		public function popItem():void
		{
			var tf:TextField = items.pop();
			this.nextY -= tf.height + linePadding;
			this.removeChild( tf );
			this.dispatchEvent( new Event("changeSize") );
		}
		
		public function clear():void
		{
			for each( var item:TextField in this.items )
			{
				this.removeChild( item );
			}
			
			this.items.length = 0;
			this.nextY = 0;
			this.dispatchEvent( new Event("changeSize") );
		}
		
		public function setItem( i:int, val:String ):void
		{
			this.items[ i ].text = val;
		}
		
		public function getItem( i:int ):String
		{
			return this.items[ i ].text;
		}
	}
}